package model;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.PreparedStatement;

import javax.faces.bean.*;

@ManagedBean (name="userBean")
@SessionScoped
public class UserBean implements Serializable {
   private long user_id;
   private AccountInfo accountInfo;
   private DemographicInfo demographicInfo;
   private EducationInfo educationInfo;
   private PreferenceInfo preferenceInfo;
   
   public UserBean () {
      this.user_id = 0;
      this.accountInfo = null;
      this.demographicInfo = null;
      this.educationInfo = null;
      this.preferenceInfo = null;

   }

   public UserBean (AccountInfo accountInfo, DemographicInfo demographicInfo,
                    EducationInfo educationInfo, PreferenceInfo preferenceInfo)
   {
      this.user_id = 0;
      this.accountInfo = accountInfo;
      this.demographicInfo = demographicInfo;
      this.educationInfo = educationInfo;
      this.preferenceInfo = preferenceInfo;
   }

   public long getUser_id() {
      return user_id;
   }
   public void setUser_id(long user_id) {
      this.user_id = user_id;
   }

   public AccountInfo getAccountInfo() {
      return accountInfo;
   }
   public void setAccountInfo(AccountInfo newAccountInfo) {
      this.accountInfo = newAccountInfo;
   }

   public DemographicInfo getDemographicInfo() {
      return demographicInfo;
   }
   public void setDemographicInfo(DemographicInfo newDemographicInfo) {
      this.demographicInfo = newDemographicInfo;
   }

   public EducationInfo getEducationInfo() {
      return educationInfo;
   }
   public void setEducationInfo(EducationInfo newEducationInfo) {
      this.educationInfo = newEducationInfo;
   }

   public PreferenceInfo getPreferenceInfo() {
      return preferenceInfo;
   }
   public void setPreferenceInfo(PreferenceInfo newPreferenceInfo) {
      this.preferenceInfo = newPreferenceInfo;
   }

////////////////////////////////////////////////////////////////////////

   public String saveAccountInfo() {
/*
      MySQLInfo mysqlinfo = new MySQLInfo();
      String DB_URL = mysqlinfo.getDBUrl();

      String lo_password = this.accountInfo.getPassword();

      String sql = "UPDATE " + mysqlinfo.getDbName() + "." + mysqlinfo.getTable() + " SET PASSWORD='" + lo_password + "' WHERE CUSTOMER_ID=" + this.user_id + ";";
      PreparedStatement pst = null;
      Connection con = getConnection(DB_URL);

      try {
         pst = con.prepareStatement(sql);
         pst.executeUpdate();

         pst.close();
         con.close();

      } catch (SQLException e) {
         e.printStackTrace();
      }
*/
      return("MainPage");
   }

////////////////////////////////////////////////////////////////////////

   public String saveDemographicInfo() {
  /*
      MySQLInfo mysqlinfo = new MySQLInfo();
      String DB_URL = mysqlinfo.getDBUrl();

      String lo_password = this.accountInfo.getPassword();

      String sql = "UPDATE " + mysqlinfo.getDbName() + "." + mysqlinfo.getTable() + " SET PASSWORD='" + lo_password + "' WHERE CUSTOMER_ID=" + this.user_id + ";";
      PreparedStatement pst = null;
      Connection con = getConnection(DB_URL);

      try {
         pst = con.prepareStatement(sql);
         pst.executeUpdate();

         pst.close();
         con.close();

      } catch (SQLException e) {
         e.printStackTrace();
      }
*/
      return("MainPage");
   }

////////////////////////////////////////////////////////////////////////

   public String saveEducationInfo() {
/*
      MySQLInfo mysqlinfo = new MySQLInfo();
      String DB_URL = mysqlinfo.getDBUrl();

      String lo_password = this.accountInfo.getPassword();

      String sql = "UPDATE " + mysqlinfo.getDbName() + "." + mysqlinfo.getTable() + " SET PASSWORD='" + lo_password + "' WHERE CUSTOMER_ID=" + this.user_id + ";";
      PreparedStatement pst = null;
      Connection con = getConnection(DB_URL);

      try {
         pst = con.prepareStatement(sql);
         pst.executeUpdate();

         pst.close();
         con.close();

      } catch (SQLException e) {
         e.printStackTrace();
      }
*/
      return("MainPage");
   }

////////////////////////////////////////////////////////////////////////

   public String savePreferenceInfo() {
/*
      MySQLInfo mysqlinfo = new MySQLInfo();
      String DB_URL = mysqlinfo.getDBUrl();

      String lo_password = this.accountInfo.getPassword();

      String sql = "UPDATE " + mysqlinfo.getDbName() + "." + mysqlinfo.getTable() + " SET PASSWORD='" + lo_password + "' WHERE CUSTOMER_ID=" + this.user_id + ";";
      PreparedStatement pst = null;
      Connection con = getConnection(DB_URL);

      try {
         pst = con.prepareStatement(sql);
         pst.executeUpdate();

         pst.close();
         con.close();

      } catch (SQLException e) {
         e.printStackTrace();
      }
*/
      return("MainPage");
   }

////////////////////////////////////////////////////////////////////////

   public Connection getConnection(String DB_URL) {
      Connection con = null;

      try {
         con = DriverManager.getConnection(DB_URL);
         System.out.println("Connection completed.");
      } catch (SQLException ex) {
         System.out.println(ex.getMessage());
      }
      finally { }

      return con;
   }


}